'''
Created on Feb 13, 2014

@author: yfrank
'''

import CodingUtils.Logging as Logging

class StateMachine(object):
    '''
    classdocs
    '''

    def __init__(self,peer_,state_=None):

        self.peer = peer_
        self.state = state_

    def get_state(self):
        return self.state.get_type()


    def switch_to_next_state(self,next_state,event=None):
        if (self.state != next_state):
            msg = "state switching "+str(self.state)+"->"+str(next_state)
            Logging.log(msg)
            self.state = next_state


    def handle_event(self,event,data=None):
        prev = self.state
        msg = str(prev)+" processing "+str(event)
        Logging.log(msg)
        if not self.state.process_event(event,data):
            msg = str(prev)+" failed to process "+str(event)
            Logging.log(msg)

